/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author Hai-PC
 */
public class MyUtil {

    /**
     *
     * @param lastID
     * @param prefixID
     * @return Next ID
     */
    public static String NextID(String lastID, String prefixID) {
        if (!lastID.equals("")) {
            int nextID = Integer.parseInt(lastID.substring(prefixID.length(), lastID.length())) + 1;
            int lengNumberID = lastID.length() - prefixID.length();
            String zeroNumber = "";
            for (int i = 1; i < lengNumberID + 1; i++) {
                if (nextID < Math.pow(10, i)) {
                    for (int j = 1; j < lengNumberID - i + 1; i++) {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + String.valueOf(nextID);
                }
            }
            return prefixID + String.valueOf(nextID);
        } else {
            switch (prefixID) {
                case "DE": // Department
                    return prefixID + "01";
                case "SE": // Service
                    return prefixID + "001";
                case "SH": // Shift
                    return prefixID + "1";
                case "CA": // Category
                    return prefixID + "01";
                case "MA": // Manufacturer
                    return prefixID + "001";
                case "AR": // Article
                    return prefixID + "00000001";
                case "CO": // Color
                    return prefixID + "1";
                case "VE": //Vendor
                    return prefixID + "001";
                case "STO": // Store
                    return prefixID + "01";
                case "PR": // Product
                    return prefixID + "001";
                case "CU": // Customer
                    return prefixID + "0001";
                case "STA": // Staff
                    return prefixID + "001";
                case "SL": // Slide360
                    return prefixID + "001";
                case "RE": // Receipt Note
                    return prefixID + "00000001";
                case "DEL": // Delivery Note
                    return prefixID + "0000001";
                case "DND": // Delivery Note Details
                    return prefixID + "0000001";
                case "TR": // Transfer Note
                    return prefixID + "00000001";
                case "SCH": //Schedule
                    return prefixID + "001";
                case "OR": // Old Receipt Note
                    return prefixID + "00000001";
                case "ORD": // Old Receipt Note Details
                    return prefixID + "0000001";
                case "OP": // Old Product
                    return prefixID + "0001";
                case "PV": // Payment Voucher
                    return prefixID + "00000001";
                case "PD": // Payment Voucher Details
                    return prefixID + "00000001";
                case "WN": // Warranty Note
                    return prefixID + "00000001";
                case "SN": // Service Note
                    return prefixID + "00000001";
                case "SND": // Service Note Details
                    return prefixID + "0000001";
                case "MO": // Module
                    return prefixID + "001";
                case "CN": // Change Note Details
                    return prefixID + "001";
            }
        }
        return null;
    }

    /**
     * Convert String to MD5;
     *
     * @param ss
     * @return
     */
    public static String StringtoMD5(String ss) {
        try {
            String s = ss.concat("@$491f");
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(s.getBytes());

            byte[] byteData = md.digest();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(MyUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static String baseURL() {
       // return "http://localhost:8080/MobileShop/";
         return "http://localhost:8080/TechShop/";
        //return "http://mobileshop.jelastic.servint.net/MobileShop/";
    }

    /**
     * Get A Managed Bean Object
     *
     * @param name
     * @return A Managed Bean Object
     */
    public static Object getManagedBean(String name) {
        FacesContext ctx = FacesContext.getCurrentInstance();
        return ctx.getApplication().evaluateExpressionGet(ctx, "#{" + name + "}", Object.class);
    }

    public static void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public static Date convertStringToDate(String s) {
        if ("".equals(s) || s == null) {
            s = "0:0";
        }
        int hour, minute;
        String[] arr = s.split(":");
        hour = Integer.parseInt(arr[0]);
        minute = Integer.parseInt(arr[1]);
        Date date = new Date();
        date.setHours(hour);
        date.setMinutes(minute);
        return date;
    }

    public static int convertDayToInt(String s) {
        int i = 10;
        switch (s) {
            case "Thu 2":
                i = 1;
                break;
            case "Thu 3":
                i = 2;
                break;
            case "Thu 4":
                i = 3;
                break;
            case "Thu 5":
                i = 4;
                break;
            case "Thu 6":
                i = 5;
                break;
            case "Thu 7":
                i = 6;
                break;
            case "CN":
                i = 0;
                break;
        }
        return i;
    }
}
